package org.halk.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.halk.entity.Book;
import org.halk.mapper.BookMapper;
import org.halk.service.BookService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class BookServiceImpl  extends ServiceImpl<BookMapper, Book> implements BookService {
    @Resource
    private BookMapper bookMapper;

    private static final String TXT_PATH = "H:\\test\\仙界修仙.txt";


    @Override
    public void reade() throws IOException {
        List<String> lines = Files.readAllLines(Paths.get(TXT_PATH));

        Map<String, StringBuilder> chapters = new LinkedHashMap<>();
        String currentChapter = null;
        StringBuilder chapterContent = new StringBuilder();
        ArrayList<Book> resultList = new ArrayList<>();

        for (int i = 0; i < lines.size(); i++) {
            String line = lines.get(i);
            Book book = new Book().setName((i + 1) + "")
                    .setContent(line)
                    .setCreateTime(LocalDateTime.now());
            resultList.add(book);
            this.baseMapper.insert(book);


//            if (isChapterHeader(line)) { // 判断是否是章节标题
//                if (currentChapter != null) {
//                    chapters.put(currentChapter, chapterContent);
//                }
//                currentChapter = line.trim();
//                chapterContent = new StringBuilder();
//            } else {
//                chapterContent.append(line).append("\n");
//            }
        }



        // 添加最后一个章节
        if (currentChapter != null) {
            chapters.put(currentChapter, chapterContent);
        }

//        // 输出各章节内容
//        for (Map.Entry<String, StringBuilder> entry : chapters.entrySet()) {
//            System.out.println("章节: " + entry.getKey());
//            System.out.println(entry.getValue().toString());
//            System.out.println("----------------------------");
//
//            Book book = new Book().setName("章节: " + entry.getKey())
//                    .setContent(entry.getValue().toString())
//                    .setCreateTime(LocalDateTime.now());
//            this.bookMapper.insert(book);
//        }

    }
}
